#include <stdlib.h>

typedef struct s_list_elem
{
    int data;
    struct s_list_elem *next;
}list_elem_t;

typedef struct s_list
{
    list_elem_t  *first;
    int size;
}list_t;

void listInit( list_t *list)
{
    list->size = 0;
    list->first = NULL;
}

int listPush_front( list_t *list, int data)
{
    list_elem_t *new  = malloc( sizeof( list_elem_t) );
    if ( new == NULL)
    {
        return 0;
    }
    if ( list->size == 0)
    {
        new->data = data;
        new->next = NULL;
        list->first = new;

        list->size++;
        return 1;
    }

    new->data = data;
    new->next = list->first;
    list->first = new;

    list->size++;

    return 1;
}

int listPop_front( list_t *list, int *data)
{
    if ( ( list == NULL) || ( list->size == 0) || ( list->first == NULL))
    {
        return 0;
    }
    *data = list->first->data;

    list_elem_t  *old_first  = list->first;
    list->first = list->first->next;

    free( old_first);
    list->size--;

    return 1;
}

int  listIterator( list_t *list, list_elem_t **iterator)
{
    if( ( list == NULL) || ( list->size < 1) )
    {
        return 0;
    }
    *iterator = list->first;
    return 1;
}
int  listNext( list_elem_t **iterator)
{
    if ( *iterator == NULL)
    {
        return 0;
    }
    if ( (*iterator)->next == NULL)
    {
        return -1;
    }
    *iterator = (*iterator)->next;
    return 1;
}

int listDelElem( list_t *list, list_elem_t **iterator)
{
    if ( ( list == NULL) || ( *iterator == NULL) || ( iterator == NULL))
    {
        return 0;
    }
    list_elem_t *tmp_iter;
    list_elem_t *prev_elem;
    listIterator( list, &tmp_iter );

    if ( *iterator == list->first)
    {

        list_elem_t *old_first = list->first;
        list->first = list->first->next;

        *iterator = list->first;
        
        free( old_first);
        old_first = NULL;
        
        list->size--;
        return 1;
    }

    while ( tmp_iter != *iterator )
    {
        prev_elem = tmp_iter;
        if ( listNext( &tmp_iter) == -1)
        {
            return -1;
        }
    }

    prev_elem->next = tmp_iter->next;
    free( tmp_iter);
    list->size--;
    return 1;
}


int listSize( const list_t *list)
{
    if ( ( list == NULL) || ( list->first == NULL))
    {
        return -1;
    }
    return list->size;
}
void listDel( list_t *list )
{
    int tmp;
    while ( listPop_front( list, &tmp));
    list->size = -1;
    list->first = NULL;
}
